package com.choupin.choupin.admin.service.impl;

import com.choupin.choupin.admin.entity.LotteryAndTurntable;
import com.choupin.choupin.admin.mapper.ManageLotteryMapper;
import com.choupin.choupin.admin.mapper.ManageTurntableMapper;
import com.choupin.choupin.admin.service.IManageTurntableService;
import com.choupin.choupin.admin.service.exception.DeleteException;
import com.choupin.choupin.entity.Lottery;
import com.choupin.choupin.entity.Turntable;
import com.choupin.choupin.service.exception.ParameterIsEmptyException;
import com.choupin.choupin.service.exception.SelectExcepion;
import com.choupin.choupin.service.exception.UpdateException;
import com.choupin.choupin.utils.images.SaveImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("ALL")
@Service
public class ManageTurntableServiceImpl implements IManageTurntableService {
    private  static final  String LOCAL_UPLOAD_PATH="D:/choupin/src/main/resources/static/upload/admin/turntables";
    private  static final  String LOCAL_FRONT_PATH="D:/uniapp/choupinSaveImage/static/turntables/";

     @Autowired
    ManageTurntableMapper manageTurntableMapper;

     @Autowired
    ManageLotteryMapper manageLotteryMapper;

    @Override
    public List<LotteryAndTurntable> showTurntables(Integer currentPage, Integer pagePize) {
        //        参数校验
        if (currentPage==null||pagePize==null){
            throw new ParameterIsEmptyException("参数为空！");
        }
//        查询转盘列表
        List<Turntable> turntables=manageTurntableMapper.showTurntables(currentPage,pagePize);
        if (turntables==null||turntables.size()<=0){
            throw new SelectExcepion("查询转盘表异常！");
        }
//        存储返回数据
        List<LotteryAndTurntable> lotteryAndTurntables=new ArrayList<>();
//        遍历数据
        for (Turntable t:turntables){
//            查出抽奖池名称
            Lottery lottery=manageLotteryMapper.selectLotteryById(t.getPoolId());
            LotteryAndTurntable lotteryAndTurntable=new LotteryAndTurntable();
            lotteryAndTurntable.setId(t.getId());
            lotteryAndTurntable.setPrizeName(t.getPrizeName());
            lotteryAndTurntable.setAmount(t.getAmount());
            lotteryAndTurntable.setImage(t.getImage());
            lotteryAndTurntable.setTurntableId(t.getTurntableId());
            lotteryAndTurntable.setExchangeTime(t.getExchangeTime());
            lotteryAndTurntable.setPoolId(t.getPoolId());
            lotteryAndTurntable.setName(lottery.getName());
            lotteryAndTurntables.add(lotteryAndTurntable);
        }
        System.out.println("查询转盘数据成功！"+lotteryAndTurntables);

        return lotteryAndTurntables;
    }

    @Override
    public List<LotteryAndTurntable> selectTurntables(String id, String prizeName, String name) {

        String idStr=null;
        Integer iid=null;
         if(id!=null&&id!=""){
             idStr="ture";
             iid=Integer.parseInt(id);
         }
//       联合查询
        List<LotteryAndTurntable> lotteryAndTurntables=manageTurntableMapper.selectTurntables(iid,idStr,prizeName,name);
        if (lotteryAndTurntables==null||lotteryAndTurntables.size()<=0){
            throw new SelectExcepion("查询异常！");
        }
        System.out.println("联合查询转盘信息成功！"+lotteryAndTurntables);
        return lotteryAndTurntables;
    }

    @Override
    public void deleteTurntables(Integer id) {
//          参数校验
         if (id==null){
             throw new ParameterIsEmptyException("参数为空！");
         }
         Integer result=manageTurntableMapper.deleteTurntable(id);
         if (result!=1||result==null){
             throw new DeleteException("删除转盘分片异常！");
         }
        System.out.println("删除转盘分片成功！"+id);
    }

    @Override
    public Integer addTurntables(Turntable turntable,String phone) {
        if (turntable==null||phone==null){
            throw new ParameterIsEmptyException("参数为空！");
        }
        LocalDateTime localDateTime=LocalDateTime.now();
        turntable.setCreatedUser(phone);
        turntable.setModifiedUser(phone);
        turntable.setCreatedTime(localDateTime);
        turntable.setModifiedTime(localDateTime);
//        更新数据库
        manageTurntableMapper.addTurntables(turntable);
        System.out.println("添加转盘分片成功！");
        Turntable turntable1=manageTurntableMapper.selectById(turntable.getTurntableId(),turntable.getPoolId());
        return turntable1.getId();
    }

    @Override
    public void updateImage(Integer id, MultipartFile file, String phone) {
        if (id==null||file==null||phone==null){
            throw new ParameterIsEmptyException("参数为空！");
        }
//        保存图片
        String image= SaveImage.saveImage(file,LOCAL_UPLOAD_PATH,LOCAL_FRONT_PATH,"http://10.102.228.179:5173/static/turntables/");
        if (image==null){
            throw new UpdateException("保存图片失败");
        }
        LocalDateTime now=LocalDateTime.now();
        Integer result=manageTurntableMapper.updateImage(id,image,phone,now);
        if (result==null){
            throw new UpdateException("保存图片失败");
        }
        System.out.println("更新抽奖池图片成功！");


    }

    @Override
    public Turntable showUpdate(Integer id) {
//        参数校验
        if (id==null){
            throw new ParameterIsEmptyException("参数为空！");
        }
        Turntable turntable=manageTurntableMapper.showUpdate(id);
        if (turntable==null){
            throw new SelectExcepion("转盘不存在！");
        }
        return turntable;
    }

    @Override
    public void updateTurntable(Integer id, String prizeName, String amount, String exchangeTime,String phone) {
        if (id==null){
            throw new ParameterIsEmptyException("参数为空！");
        }
        Long lamount=null;
        LocalDateTime endTime=null;
//     查出转盘数据
        Turntable turntable=manageTurntableMapper.showUpdate(id);
        if (turntable==null){
            throw new SelectExcepion("转盘分片不存在！");
        }
        if (prizeName!=null&&prizeName!=""){
            if (prizeName.equals(turntable.getPrizeName())){
                prizeName=null;
            }
        }
        if (amount!=null&&amount!=""){
            lamount=Long.parseLong(amount);
            if (lamount==turntable.getAmount()){
                amount=null;
            }
        }
        if (exchangeTime!=null&&exchangeTime!=""){
            //        时间转换
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            endTime=LocalDateTime.parse(exchangeTime.replace("T"," "),formatter);
            if (endTime.isEqual(turntable.getExchangeTime())){
                exchangeTime=null;
            }
        }
        LocalDateTime localDateTime=LocalDateTime.now();
//        更新转盘数据
        Integer result=manageTurntableMapper.updateTurntable(id,prizeName,lamount,amount,endTime,exchangeTime,phone,localDateTime);
        if (result==null){
            throw new UpdateException("转盘信息更新失败！"+id);
        }
        System.out.println("转盘信息更新成功！");
    }
}
